/*
 * timestamp.h
 *
 *  Created on: 2008-9-5
 *      Author: fanliangliang
 */

#ifndef TIMESTAMP_H_
#define TIMESTAMP_H_

#include "config.h"
#include <string>
#include <iosfwd>

BEGIN_NAMESPACE_TOXIC

using std::string;
using std::ostream;

class Timestamp {
public:
	explicit Timestamp(time_t seconds = 0);
	explicit Timestamp(const string& str);
	~Timestamp();

	int time() const;
	int year() const;
	int month() const;
	int day() const;
	int hour() const;
	int minute() const;
	int second() const;

	void setTime(int time);
	void setYear(int year);
	void setMonth(int month);
	void setDay(int day);
	void setHour(int hour);
	void setMinute(int minute);
	void setSecond(int second);

	string toString(const char* format = "%Y-%m-%d %H:%M:%S") const;

public:
	static Timestamp startTime();

private:
	static Timestamp _startTime;

private:
	time_t _time;
};


inline ostream& operator<<(ostream& strm, const Timestamp& ts) {
	return strm << ts.toString();
}

inline bool operator<(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() < rhs.time();
}

inline bool operator>(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() > rhs.time();
}

inline bool operator==(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() == rhs.time();
}

inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() != rhs.time();
}

inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() <= rhs.time();
}

inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs) {
	return lhs.time() >= rhs.time();
}

END_NAMESPACE_TOXIC

#endif /* TIMESTAMP_H_ */
